﻿//************************************************************************************************
// Copyright © 2012 Steven M. Cohn. All Rights Reserved.
//
// Facility:	Microsoft Development Environment 2012  Version 11.0.50727.1
// Environment:	Microsoft .NET Framework 4.5            Version v4.0.30319
//
// Description: Registers or unregisters this app as an IE Tools menu add-on
//
// Revision History:
// -Who--------------- -When---------- -What------------------------------------------------------
// Steven M. Cohn		27-Nov-2012		New
//************************************************************************************************

namespace CookieChipper
{
	using System;
	using System.Reflection;
	using Microsoft.Win32;


	/// <summary>
	/// 
	/// </summary>

	internal static class Installer
	{
		private static readonly Guid AppID;
		private static readonly string RegistryPath;

		private const string MenuTextKey = "MenuText";
		private const string MenuStatusBarKey = "MenuStatusBar";
		private const string ExecKey = "Exec";
		private const string CLSIDKey = "CLSID";
		private const string ToolbarExtExecID = "{1FBA04EE-3024-11d2-8F1F-0000F87ABD16}";

		/*
		 * HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer\Extensions\{GUID}
		 *	\CLSID = 1FBA04EE-3024-11d2-8F1F-0000F87ABD16   <-- "CLSID_Shell_ToolbarExtExec"
		 *	\MenuText = "CookieChipper..."
		 *	\MenuCustomize = "Tools|Help"                   <-- default is Tools (so not needed)
		 *	\MenuStatusBar = "Open the CookieChipper cookie manager"
		 *	\Exec = path to executable
		 *	
		 */

		static Installer ()
		{
			AppID = new Guid("E56CBB8E-9FEE-4139-8059-CFED9CCAE705");
			RegistryPath = @"Software\Microsoft\Internet Explorer\Extensions\" + AppID.ToString("B");
		}


		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>

		public static bool IsInstalled ()
		{
			bool installed = false;

			try
			{
				using (var key = Registry.CurrentUser.OpenSubKey(RegistryPath))
				{
					installed = (key != null);
				}
			}
			catch
			{
			}

			return installed;
		}


		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>

		public static bool Install ()
		{
			try
			{
				using (RegistryKey key = Registry.CurrentUser.CreateSubKey(RegistryPath))
				{
					if (key != null)
					{
						key.SetValue(MenuTextKey, "CookieChipper");
						key.SetValue(MenuStatusBarKey, "Open the CookieChipper cookie manager");
						key.SetValue(ExecKey, Assembly.GetExecutingAssembly().CodeBase);
						key.SetValue(CLSIDKey, ToolbarExtExecID);
					}
				}
			}
			catch
			{
				return false;
			}

			return true;
		}


		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>

		public static bool Uninstall ()
		{
			try
			{
				Registry.CurrentUser.DeleteSubKey(RegistryPath, false);
			}
			catch
			{
				return false;
			}

			return true;
		}
	}
}
